﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.Common;
using DevExpress.XtraEditors.Controls;
using Waker.APIs.ADS;
using DevExpress.XtraEditors;
using Waker.Entitys;

namespace Waker.Devices
{
    public partial class FormTimePeriodEdit : Base.FormBasePop
    {

        DataTable _DtSource;
        byte _ID;
        public FormTimePeriodEdit(byte id)
        {
            InitializeComponent();
            this.Text += id < 1 ? "添加" : "修改";
            _ID = id;
            this.Load += new EventHandler(FormTimePeriodEdit_Load);
        }

        private void FormTimePeriodEdit_Load(object sender, EventArgs e)
        {
            var bal = new BAL.BAL_Device();
            _DtSource = bal.GetTimePeriodByID(_ID);
            lueDevice_ID.Properties.DataSource = bal.GetAllDeviceIDAndName();
            ControlUtil.FillLookUpEdit(bal.GetAllDeviceIDAndName(), lueDevice_ID, "Device_Name", "Device_ID");
            if (_DtSource.Rows.Count < 1)
            {
                this.BindingContext[_DtSource].AddNew();
                this.BindingContext[_DtSource].EndCurrentEdit();
                var dr = _DtSource.Rows[0];
                dr["BeginDate"] = new DateTime(2000, 1, 1);
                dr["EndDate"] = new DateTime(2099, 1, 1);
                foreach (DataColumn col in _DtSource.Columns)
                {
                    if (!col.ColumnName.StartsWith("StartTime") && !col.ColumnName.StartsWith("Endtime"))
                        continue;
                    dr[col.ColumnName] = new DateTime(2000, 1, 1);
                }
                checkEdit1.Checked =
                    checkEdit2.Checked =
                    checkEdit3.Checked =
                    checkEdit4.Checked =
                    checkEdit5.Checked =
                    checkEdit6.Checked =
                    checkEdit7.Checked = true;
            }
            else
            {
                var dr = _DtSource.Rows[0];
                checkEdit1.Checked = (int)(Convert.ToInt32(dr["ValidWeek"]) & (int)ADSHalConstant.ADS_WeekMask.ADS_WM_MON)
                   == (int)ADSHalConstant.ADS_WeekMask.ADS_WM_MON;
                checkEdit2.Checked = (int)(Convert.ToInt32(dr["ValidWeek"]) & (int)ADSHalConstant.ADS_WeekMask.ADS_WM_TUES)
                        == (int)ADSHalConstant.ADS_WeekMask.ADS_WM_TUES;
                checkEdit3.Checked = (int)(Convert.ToInt32(dr["ValidWeek"]) & (int)ADSHalConstant.ADS_WeekMask.ADS_WM_WEDNES)
                        == (int)ADSHalConstant.ADS_WeekMask.ADS_WM_WEDNES;
                checkEdit4.Checked = (int)(Convert.ToInt32(dr["ValidWeek"]) & (int)ADSHalConstant.ADS_WeekMask.ADS_WM_THURS)
                        == (int)ADSHalConstant.ADS_WeekMask.ADS_WM_THURS;
                checkEdit5.Checked = (int)(Convert.ToInt32(dr["ValidWeek"]) & (int)ADSHalConstant.ADS_WeekMask.ADS_WM_FRI)
                        == (int)ADSHalConstant.ADS_WeekMask.ADS_WM_FRI;
                checkEdit6.Checked = (int)(Convert.ToInt32(dr["ValidWeek"]) & (int)ADSHalConstant.ADS_WeekMask.ADS_WM_SATUR)
                        == (int)ADSHalConstant.ADS_WeekMask.ADS_WM_SATUR;
                checkEdit7.Checked = (int)(Convert.ToInt32(dr["ValidWeek"]) & (int)ADSHalConstant.ADS_WeekMask.ADS_WM_SUN)
                        == (int)ADSHalConstant.ADS_WeekMask.ADS_WM_SUN;
            }


            ControlUtil.BindData(this.Controls, _DtSource);
        }
        protected override void FormSave()
        {
            if (_DtSource == null || _DtSource.Rows.Count < 1)
                return;
            base.errorPop.ClearErrors();
            if (lueDevice_ID.EditValue == null || lueDevice_ID.EditValue == DBNull.Value)
            {
                base.errorPop.SetError(lueDevice_ID, "请选择设备");
                return;
            }
            if (string.IsNullOrEmpty(txtTimePeriod_Name.Text))
            {
                base.errorPop.SetError(txtTimePeriod_Name, "名称不能为空");
                return;
            }
            if (dteBeginDate.EditValue == null || dteBeginDate.EditValue == DBNull.Value)
            {
                base.errorPop.SetError(dteBeginDate, "请选择开始日期");
                return;
            }
            if (dteEndDate.EditValue == null || dteEndDate.EditValue == DBNull.Value)
            {
                base.errorPop.SetError(dteEndDate, "请选择结束日期");
                return;
            }
            //var isCheck = false;
            //for (int i = 0; i < 7; i++)
            //{
            //    if (isCheck)
            //        break;
            //    var control = this.Controls.Find("checkEdit" + i, false);
            //    if (control == null || control.Length < 1)
            //        return;
            //    var chk = control[0] as CheckEdit;
            //    if (chk == null)
            //        return;
            //    isCheck = chk.Checked;
            //}
            //if (!isCheck)
            //{

            //}

            this.BindingContext[_DtSource].EndCurrentEdit();
            if (_DtSource == null || _DtSource.Rows.Count < 1)
                return;
            byte byteValidWeek = 0;
            if (checkEdit1.Checked)
            {
                byteValidWeek |= (byte)ADSHalConstant.ADS_WeekMask.ADS_WM_MON;
            }

            if (checkEdit2.Checked)
            {
                byteValidWeek |= (byte)ADSHalConstant.ADS_WeekMask.ADS_WM_TUES;
            }

            if (checkEdit3.Checked)
            {
                byteValidWeek |= (byte)ADSHalConstant.ADS_WeekMask.ADS_WM_WEDNES;
            }

            if (checkEdit4.Checked)
            {
                byteValidWeek |= (byte)ADSHalConstant.ADS_WeekMask.ADS_WM_THURS;
            }

            if (checkEdit5.Checked)
            {
                byteValidWeek |= (byte)ADSHalConstant.ADS_WeekMask.ADS_WM_FRI;
            }

            if (checkEdit6.Checked)
            {
                byteValidWeek |= (byte)ADSHalConstant.ADS_WeekMask.ADS_WM_SATUR;
            }

            if (checkEdit7.Checked)
            {
                byteValidWeek |= (byte)ADSHalConstant.ADS_WeekMask.ADS_WM_SUN;
            }
            _DtSource.Rows[0]["ValidWeek"] = byteValidWeek;

            var isOk = new BAL.BAL_Device().SaveTimePeriod(_DtSource.Rows[0]);
            if (isOk)
            {
                UI.ShowInfo("保存成功");
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                UI.ShowError("保存失败");
            }
        }

    }
}
